package manager.com.ithei.edu.info.manager.controller;

import manager.com.ithei.edu.info.manager.domain.Teacher;
import manager.com.ithei.edu.info.manager.service.TeacherService;

import java.text.ParseException;
import java.util.Scanner;

public abstract class FatherTeacherController {
    private Scanner sc = new Scanner(System.in);
    private TeacherService teacherService = new TeacherService();

    //教师主界面
    public final void start() throws ParseException {

        teacherLoop:
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    // System.out.println("添加老师");
                    addTeacher();
                    break;
                case "2":
                    // System.out.println("删除老师");
                    deleteStudentByIdTwo();
                    break;
                case "3":
                    // System.out.println("修改老师");
                    setTeacherByIdTwo();
                    break;
                case "4":
                    // System.out.println("查看老师");
                    findAllTeachers();
                    break;
                case "5":
                    System.out.println("感谢您使用老师管理系统, 再见!");
                    break teacherLoop;
                default:
                    System.out.println("您的输入有误, 请重新输入");
                    break;
            }
        }

    }

    //修改教师的方法
    public final void setTeacherByIdTwo() throws ParseException {
        String id = getId(true);
        //客服录入老师信息
        Teacher teacher = inputStudentInfo(id);
        teacherService.setTeacherByIdTwo(id, teacher);
        System.out.println("修改教师信息成功!");
    }

    //删除老师的方法
    public final void deleteStudentByIdTwo() {
        String id = getId(true);
        teacherService.deleteTeacherByIdTwo(id);
        System.out.println("删除教师成功!");
    }

    private final void findAllTeachers() {
        Teacher[] teachers = teacherService.findAllTeachers();
        if (teachers == null) {
            System.out.println("查无信息!!!");
        } else {
            System.out.println("姓名\t\t编号\t\t年龄\t\t生日\t\t地址\t\t");
            for (int i = 0; i < teachers.length; i++) {
                if (teachers[i] != null) {
                    System.out.println(teachers[i].getName() + "\t\t" + teachers[i].getSid() + "\t\t" + teachers[i].getAge() + "\t\t" + teachers[i].getBirthday() + "\t\t" + teachers[i].getAddress());
                }
            }
        }
    }

    private final void addTeacher() throws ParseException {
        String id = getId(false);
        Teacher stu = inputStudentInfo(id);
        if (teacherService.addTeacher(stu)) {
            System.out.println("添加成功了!");
        } else {
            System.out.println("添加失败了!!");
        }
    }

    //用户录入数据
     abstract Teacher inputStudentInfo(String id) throws ParseException;

    //获取id
    private final String getId(boolean bool) {
        String id;
        while (true) {
            System.out.println("输入id:");
            id = sc.next();
            boolean flag = teacherService.isExists(id);
            if (flag ^ bool) {
                System.out.println("重新输入id:");
            } else {
                break;
            }
        }
        return id;
    }
}

